package com.itshizhan.itjidancideveloper.controller.webfront;

import com.itshizhan.itjidancideveloper.dao.WordDao;
import com.itshizhan.itjidancideveloper.models.Word;
import com.itshizhan.itjidancideveloper.response.CodeMsgEnum;
import com.itshizhan.itjidancideveloper.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = {"WebFront单词相关接口"})
@RestController()
@RequestMapping("/webfront/word")
public class WordFrontController {

	@Autowired
	WordDao wordDao;

	@ApiOperation("根据关键字，查询相关单词")
	@GetMapping("/query")
	Response<List<Word>> getListByKeyword(@RequestParam("key") String keyword){
		if (keyword.length() < 3) {
			return Response.fail(CodeMsgEnum.ERROR_PARAMETER);
		}
		return Response.success(wordDao.findByWordNameLike(keyword));
	}
	@ApiOperation("根据单词Id查询单词详情")
	@GetMapping("/detail")
	Response<Word> getWordDetailById(@RequestParam("id") Long id) {
		Word word = wordDao.findById(id).orElse(null);
		return Response.success(word);
	}

}
